package cn.jeefast.modules.platform.service.impl;

import cn.jeefast.common.entity.*;
import cn.jeefast.common.utils.R;
import cn.jeefast.modules.platform.dao.*;
import cn.jeefast.modules.platform.service.MbResearchData1Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author theodo
 * @since 2020-01-06
 */
@Service
public class MbResearchData1ServiceImpl extends ServiceImpl<MbResearchData1Dao, MbResearchData> implements MbResearchData1Service {

    @Autowired
    private MbResearchData1Dao researchData1Dao;

    @Autowired
    private MbMessage1Dao message1Dao;

    @Autowired
    private MbPatient1Dao mbPatient1Dao;

    @Autowired
    private MbResearchStep1Dao mbResearchStep1Dao;

    @Autowired
    private MbResearch1Dao mbResearch1Dao;

    @Override
    public R dataDetail(Long id,Integer page, Integer limit) {
        List<Map> map;
        if(id==null){
            map = researchData1Dao.dataDetail(page, limit);
        }else{
            map = researchData1Dao.dataDetailSearch(id,page, limit);
        }
        for (Map m:map) {
            int i = 0;
            if(m.get("data_img1")!=null){
                i++;
            }
            if(m.get("data_img2")!=null){
                i++;
            }
            if(m.get("data_img3")!=null){
                i++;
            }
            if(m.get("data_img4")!=null){
                i++;
            }
            if(m.get("data_img5")!=null){
                i++;
            }
            if(m.get("data_img6")!=null){
                i++;
            }
            if(m.get("data_img7")!=null){
                i++;
            }
            if(m.get("data_img8")!=null){
                i++;
            }
            if(m.get("data_img9")!=null){
                i++;
            }
            m.put("countImg",i);
        }
        EntityWrapper ew = new EntityWrapper();
        if(id!=null){
            ew.eq("patient_id",id);
        }
        List list = researchData1Dao.selectList(ew);
        return R.ok().put("data",map).put("count",list.size());
    }

    @Override
    public R dataInfo(Long id) {
        //获取基础详情信息
        Map map = researchData1Dao.selectDataById(id);
        //根据dataID 获取患者iD
        MbResearchData mbResearchData = researchData1Dao.selectById(id);
        EntityWrapper ew = new EntityWrapper();
        ew.eq("patient_id",mbResearchData.getPatientId());
        List<MbResearchData> list = researchData1Dao.selectList(ew);
        List<Map> result = new ArrayList<>();
        for (MbResearchData md:list) {
            md.getResStepId();
            MbResearchStep mbResearchStep = mbResearchStep1Dao.selectById(md.getResStepId());
            Map m = new HashMap();
            m.put("id",md.getResStepId());
            m.put("name",mbResearchStep.getStepName());
            result.add(m);
        }
        EntityWrapper ew1 = new EntityWrapper();
        return R.ok().put("data",map).put("step",result).put("strpInfo",list);
    }

    /**
     * 审核拒绝
     * @param id
     * @param reason
     * @return
     */
    @Override
    @Transactional
    public R refuse(Long id, String reason) {
        MbResearchData mbResearchData = researchData1Dao.selectById(id);
        MbPatient mbPatient = mbPatient1Dao.selectById(mbResearchData.getPatientId());
        MbResearchStep mbResearchStep = mbResearchStep1Dao.selectById(mbResearchData.getResStepId());
        MbResearch mbResearch = mbResearch1Dao.selectById(mbResearchStep.getResId());
        mbResearchData.setState(2);
        mbResearchData.setReason(reason);
        mbResearchData.setUpdateTime(new Date());
        mbResearchData.setDataId(id);
        researchData1Dao.updateById(mbResearchData);
        //添加通知提醒
        MbMessage mbMessage = new MbMessage();
        mbMessage.setIsRead(0);
        mbMessage.setReason(reason);
        mbMessage.setSendTime(new Date());
        mbMessage.setUpTime(new Date());
        mbMessage.setTitle("患者"+mbPatient.getPatName()+"的"+mbResearchStep.getStepName()+"的调研数据审核失败：");
        //获取患者的医生ID
        MbResearchData mbResearchData1 = researchData1Dao.selectById(id);
        mbMessage.setToDoc(mbResearchData1.getDocId());
        mbMessage.setResearch(mbResearch.getResName());
        mbMessage.setToId(mbPatient.getId());
        message1Dao.insert(mbMessage);
        return R.ok();
    }
}
